/*  KDevelop 4.3.1
 *  POSTERS.cxx
 *
 * 	Author : Pallab
 * 	Created on: 12:06:21 AM - 2013:1:20
 * 	"I have not failed, I have just found 10000 ways that won't work."
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <numeric>
#include <stack>
#include <functional>
#include <bitset>
#include <iomanip>

#include <ctime>
#include <cassert>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>
#include <cstdlib>

using namespace std;


#define foR(i,frm,upto) for( int i=(frm),_i=(upto);(i) < (_i) ; ++i )
#define foD(i,frm,upto) for( int i=(frm),_i=(upto);(i) >= (_i) ; --i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define SET( ARRAY , VALUE ) memset( ARRAY , VALUE , sizeof(ARRAY) )
#define line puts("")
#define dbg(...)       fprintf(stderr, __VA_ARGS__)

inline void wait ( double seconds ) {
    double endtime = clock() + ( seconds * CLOCKS_PER_SEC );
    while ( clock() < endtime ) {
        ;
    }
}
template<class T>
    inline T fastIn() {
    register char c=0;
    register T a=0;
    bool neg=false;
    while ( c<33 ) c=getchar();
    while ( c>33 ) {
        if ( c=='-' ) {
            neg=true;
        } else {
            a= ( a*10 ) + ( c-'0' );
        }
        c=getchar();
    }
    return neg?-a:a;
}
class segment {
    public:
    int u,v,id;
    segment() :u ( 0 ),v ( 0 ),id ( 0 ) {
        ;
    }
    segment ( int u_,int v_, int id_ ) :u ( u_ ),v ( v_ ),id ( id_ ) {
        ;
    }
};
inline bool isEqual ( segment const &l, segment const &r ) {
    return l.u==r.u&&l.v==r.v;
}
class cmp {
    public:
    inline bool operator() ( segment const &l, segment const &r ) const {
        return ( l.u<r.u && l.v<r.v );
    }
};
int posterCount;
inline void read() {
    posterCount=fastIn<int>();
}
const int MX=40005;
const int inf=1023456789;
bool marked[MX];
inline void print ( set< segment,cmp > seg ) {
    foit ( it,seg ) {
        dbg ( "%d %d %d, ",it->u,it->v,it->id );
    }
    dbg ( "\n" );
}

inline void proc() {
    set< segment,cmp > segments;
    segments.insert ( segment ( 0,0,0 ) );
    segments.insert ( segment ( inf,inf,0 ) );
    set<segment> :: iterator from,to;
    foR ( i,1,posterCount+1 ) {
        int u=fastIn<int>(),v=fastIn<int>();
        segment tmp1= * ( from=segments.lower_bound ( segment ( u,u,i ) ) );
        segment tmp2= * ( to  =segments.lower_bound ( segment ( v,v,i ) ) );
        segments.erase ( from,++to );

        if ( tmp1.u<=u&&u<=tmp1.v ) {
            if ( tmp1.u<=u-1 ) {
                segments.insert ( segment ( tmp1.u,u-1,tmp1.id ) );
            }
        } else {
            if ( tmp1.u>v )
                segments.insert ( tmp1 );
        }
        if ( tmp2.u<=v&&v<=tmp2.v ) {
            if ( v+1<=tmp2.v ) {
                segments.insert ( segment ( v+1,tmp2.v,tmp2.id ) );
            }
        } else {
            if ( tmp2.u>v );
            segments.insert ( tmp2 );
        }
        segments.insert ( segment ( u,v,i ) );
    }
    //print(segments);
    SET ( marked,false );
    int cnt=0;
    foit ( it,segments ) {
        if ( it->id!=0 && !marked[it->id] ) {
            marked[it->id]=true;
            ++cnt;
        }
    }
    cout<<cnt<<'\n';
}
int main() {
    int kase = 1;
#if defined( xerxes_pc )
    freopen ( "in1", "r", stdin );
    kase = 1;
#endif
    kase=fastIn<int>();
    for ( int i=1; i<=kase; ++i ) {
        read();
        proc();
    }
    return 0;
}

// kate: indent-mode cstyle; indent-width 4; replace-tabs on; 



